package pm

import (
	"context"
	"encoding/json"
	"ez/apps/pm/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type FeeHouse struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64          `json:"id" bson:"id,omitempty"`
	House       *House         `json:"house" bson:"house"`         //所属楼栋号
	HouseId     int64          `json:"houseId" bson:"houseId"`     //所属楼栋号
	Name        string         `json:"name" bson:"name"`           //费用标题
	StartAt     time.Time      `json:"startAt" bson:"startAt"`     //开始日期
	EndAt       time.Time      `json:"endAt" bson:"endAt"`         //结束日期
	Amount      float64        `json:"amount" bson:"amount"`       //费用金额
	IsPay       bool           `json:"isPay" bson:"isPay"`         //是否付款
	PayAt       time.Time      `json:"payAt" bson:"payAt"`         //付款时间
	PayType     *PayType       `json:"payType" bson:"payType"`     //付款方式
	PayTypeId   int64          `json:"payTypeId" bson:"payTypeId"` //付款方式
	Note        string         `json:"note" bson:"note"`           //备注
	P           string         `json:"p" bson:"p"`                 //生成批次
	Order       *FeeHouseOrder `json:"order" bson:"order"`         //支付批次
	OrderId     int64          `json:"orderId" bson:"orderId"`     //支付批次
	CreateAt    time.Time      `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time      `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time     `json:"deleteAt" bson:"deleteAt"`
}

func (this *FeeHouse) DocName() string { return "FeeHouse" }
func (this *FeeHouse) GetId() int64    { return this.Id }
func (this *FeeHouse) SetId(id int64)  { this.Id = id }
func (this *FeeHouse) Create() error {
	return this.GetFactory().Create(this)
}
func (this *FeeHouse) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *FeeHouse) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *FeeHouse) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *FeeHouse) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *FeeHouse) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *FeeHouse) ToString() string {
	return string(this.ToBytes())
}
func (this *FeeHouse) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *FeeHouse) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *FeeHouse) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *FeeHouse) LoadHouse() {
	if this.HouseId == 0 {
		return
	}
	this.House, _ = NewHouseCrud().FindId(this.HouseId)
}
func (this *FeeHouse) LoadPayType() {
	if this.PayTypeId == 0 {
		return
	}
	this.PayType, _ = NewPayTypeCrud().FindId(this.PayTypeId)
}
func (this *FeeHouse) LoadOrder() {
	if this.OrderId == 0 {
		return
	}
	this.Order, _ = NewFeeHouseOrderCrud().FindId(this.OrderId)
}
func (this *FeeHouse) ClearRelationsBeforeSave() mgo.Doc {
	this.House = nil
	this.PayType = nil
	this.Order = nil
	return this
}
func neverUsed_FeeHouse() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type FeeHouseAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *FeeHouse
	Session *ez.Session
}

func NewFeeHouseAccessControl(model *FeeHouse, action string, session *ez.Session) *FeeHouseAccessControl {
	ctrl := &FeeHouseAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.FeeHouseAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
